package com.mybatis.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.mybatis.mapper.EmpMapper;

public class TestDynamicSQL {

	@Test
	public void TestIf() throws IOException{
		SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		List<Emp> emps = mapper.getEmpByMoreTJ(new Emp(1, "admin", "11@.com"));
		for (Emp emp : emps) {
			System.out.println(emp);
		}
	}
	
	@Test
	public void TestInsert() throws IOException{
		SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		Emp emp = new Emp();
		emp.setEname("小王");
//		emp.setEmail("123");
		mapper.insertEmp(emp);
	}
	
	@Test
	public void TestDelete() throws IOException{
		SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		mapper.deleteMoreEmp("21,22,23,24");
	}
	
	@Test
	public void TestDeleteByList() throws IOException{
		SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		List<Integer> ids = new  ArrayList<Integer>();
		ids.add(7);
		ids.add(8);
		mapper.deleteEmpByList(ids);
	}
	
	
	
	@Test
	public void TestChoose() throws IOException{
		SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		
		List<Emp> emps = mapper.getEmpListByChoose(new Emp(null, "admin", null));
		for (Emp emp : emps) {
			System.out.println(emp);
		}
	}
	
	public SqlSessionFactory sqlSessionFactory() throws IOException{
		InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		return sqlSessionFactory;
	}
}
